﻿using System;
using System.Collections.Generic;
using System.Linq;

public class OrderDetails
{
    public string ProductName { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; }//这是单价

    public override bool Equals(object obj)
    {
        if (obj is OrderDetails other)
        {
            return ProductName == other.ProductName && Quantity == other.Quantity && Price == other.Price;
        }
        return false;
    }

    public override int GetHashCode()
    {
        return HashCode.Combine(ProductName, Quantity, Price);
    }

    public override string ToString()
    {
        return $"Product: {ProductName}, Quantity: {Quantity}, Price: {Price}";
    }
}

public class Order
{
    public int OrderId { get; set; }
    public string Customer { get; set; }
    public List<OrderDetails> Details { get; set; } = new List<OrderDetails>();

    public decimal TotalAmount => Details.Sum(d => d.Quantity * d.Price);

    public override bool Equals(object obj)
    {
        if (obj is Order other)
        {
            return OrderId == other.OrderId && Customer == other.Customer && Details.SequenceEqual(other.Details);
        }
        return false;
    }

    public override int GetHashCode()
    {
        return HashCode.Combine(OrderId, Customer, Details);
    }

    public override string ToString()
    {
        return $"Order ID: {OrderId}, Customer: {Customer}, Total Amount: {TotalAmount}\nDetails:\n{string.Join("\n", Details)}";
    }
}

public class OrderService
{
    private List<Order> orders = new List<Order>();

    public void AddOrder(Order order)
    {
        if (orders.Any(o => o.OrderId == order.OrderId))
        {
            throw new InvalidOperationException("Order with the same ID already exists.");
        }
        orders.Add(order);
    }

    public void RemoveOrder(int orderId)
    {
        var order = orders.FirstOrDefault(o => o.OrderId == orderId);
        if (order == null)
        {
            throw new InvalidOperationException("Order not found.");
        }
        orders.Remove(order);
    }

    public void UpdateOrder(Order order)
    {
        var existingOrder = orders.FirstOrDefault(o => o.OrderId == order.OrderId);
        if (existingOrder == null)
        {
            throw new InvalidOperationException("Order not found.");
        }
        orders.Remove(existingOrder);
        orders.Add(order);
    }

    public List<Order> QueryOrders(Func<Order, bool> predicate = null)
    {
        var query = orders.AsQueryable();
        if (predicate != null)
        {
            query = query.Where(predicate).AsQueryable();
        }
        return query.OrderBy(o => o.TotalAmount).ToList();
    }

    public List<Order> GetAllOrders()
    {
        return orders.OrderBy(o => o.OrderId).ToList();
    }

    public void SortOrders(Comparison<Order> comparison = null)//默认是null，按照ID排序，可以自定义排序方法
    {
        if (comparison == null)
        {
            orders.Sort((o1, o2) => o1.OrderId.CompareTo(o2.OrderId));
        }
        else
        {
            orders.Sort(comparison);
        }
    }
}